const storageService = require('./storage')
const shortcutModeLightConfig = {
  freshwater_lamp: {
    names: ['绿草模式', '正白模式', '红草模式', '辣椒榕模式', '清爽模式', '淡雅模式', '美艳模式'],
    defaultIndex: 1,
    maxValue: [{ // 绿草模式
        R: 409,
        G: 1024,
        B: 364,
        W: 988
      },
      { // 正白模式
        R: 726,
        G: 1024,
        B: 466,
        W: 1011
      },
      { // 红草模式
        R: 1024,
        G: 835,
        B: 567,
        W: 809
      },
      { // 辣椒榕模式
        R: 923,
        G: 461,
        B: 1024,
        W: 423
      },
      { // 清爽模式
        R: 923,
        G: 461,
        B: 1024,
        W: 973
      },
      { // 淡雅模式
        R: 753,
        G: 1024,
        B: 507,
        W: 522
      },
      { // 美艳模式
        R: 753,
        G: 1024,
        B: 507,
        W: 940
      }
    ]
  },
  seawater_lamp: {
    names: ['SPS模式', 'LPS模式', 'SPS\\LPS混养'],
    defaultIndex: 0,
    maxValue: [{ // SPS模式
        R: 170,
        G: 105,
        B: 1023,
        W: 515,
        LB: 716,
        RB: 1023,
        P: 921,
        UV: 920
      },
      { // LPS模式
        R: 341,
        G: 341,
        B: 682,
        W: 1023,
        LB: 855,
        RB: 910,
        P: 795,
        UV: 341
      },
      { // SPS\\LPS混养
        R: 341,
        G: 341,
        B: 1023,
        W: 716,
        LB: 716,
        RB: 1023,
        P: 818,
        UV: 716
      }
    ],
    modes: [

    ]
  }
}



function getMaxValues(index, deviceType) {
  const dt = getDeviceType(deviceType)
  const config = shortcutModeLightConfig[dt]
  const maxValue = config.maxValue
  const obj = maxValue[index]
  const res = {}
  for (var key in obj) {
    res[key] = parseInt(obj[key] * 100 / 1024)
  }
  return res
}

function getModeNames(deviceType) {
  const dt = getDeviceType(deviceType)
  return shortcutModeLightConfig[dt].names
}

function getDeviceType(deviceType) {
  const dt = deviceType == "freshwater_diy_lamp" ? "freshwater_lamp" : deviceType
  return dt
}

/**
 * 获取模式名称
 * @param {string} deviceType 
 * @returns  {array} 模式名称
 */
function getModeOptions(deviceType) {
  const modeNames = getModeNames(deviceType)
  const modeOptions = []
  for (let i = 0; i < modeNames.length; i++) {
    modeOptions.push({
      name: modeNames[i],
      modeCode: `m${(i+1)}`,
      modeType: 'shortcut_mode'
    })
  }
  return modeOptions
}

/**
 * 获取玩家模式灯光值
 * @param {number} index 
 * @param {number} brightness 
 * @param {string} deviceType 
 */
function getValsByIdx(index, brightness, deviceType) {
  // 确保亮度在0-100范围内
  const safetyBrightness = Math.min(100, Math.max(0, brightness))
  // 计算精确的亮度百分比
  const percent = safetyBrightness / 100
  const maxObj = getMaxValues(index, deviceType)
  const vals = []
  for (var key in maxObj) {
    vals.push(Math.round(maxObj[key] * percent))
  }
  return vals
}

module.exports = {
  getMaxValues,
  getModeNames,
  //getModeOptions,
  getValsByIdx,
}